package com.ibm.risk.irmp.base.auth.service.impl;

import com.ibm.risk.irmp.base.auth.dao.SystemUserRepository;
import com.ibm.risk.irmp.base.auth.entity.SystemBizBranch;
import com.ibm.risk.irmp.base.auth.entity.SystemMngBranch;
import com.ibm.risk.irmp.base.auth.entity.SystemUser;
import com.ibm.risk.irmp.base.auth.service.IOrgUserBaseService;
import com.ibm.risk.irmp.common.auth.vo.IUserInfoVo;
import com.ibm.risk.irmp.common.auth.vo.OrgInfoVo;
import com.ibm.risk.irmp.common.auth.vo.UserInfoVo;
import jakarta.persistence.EntityManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Primary
@Slf4j
@Transactional(readOnly = true)
public class OrgUserBaseServiceImpl implements IOrgUserBaseService {
    @Autowired
    private SystemUserRepository userRepository;
    @Autowired
    private EntityManager entityManager;
    @Override
    public OrgInfoVo getBizOrgInfo(String branchCode) {
        SystemBizBranch branch = entityManager.find(SystemBizBranch.class, branchCode);
        OrgInfoVo vo = new OrgInfoVo();
        if (branch != null) {
            BeanCopier.create(SystemBizBranch.class, OrgInfoVo.class, false).copy(branch, vo, null);
        } else {
            vo.setBranchCode(branchCode);
            vo.setBranchName(branchCode);
        }
        return vo;
    }

    @Override
    public OrgInfoVo getMngOrgInfo(String branchCode) {
        SystemMngBranch branch = entityManager.find(SystemMngBranch.class, branchCode);
        OrgInfoVo vo = new OrgInfoVo();
        vo.setBranchCode(branch.getBranchCode());
        vo.setBranchName(branch.getBranchName());
        vo.setBelongLevel(branch.getBelongLevel());
        return vo;
    }

    @Override
    public IUserInfoVo getUserInfoByUserName(String username) {
        SystemUser user = userRepository.findById(username).orElse(null);
        UserInfoVo vo = new UserInfoVo();
        if (user == null) {
            vo.setDisplayName(username);
            vo.setUsername(username);
        } else {
            userCopier.copy(user, vo, null);
        }
        return vo;
    }
    BeanCopier userCopier = BeanCopier.create(SystemUser.class, UserInfoVo.class, false);
    @Override
    public IUserInfoVo getUserInfoBySn(String userSn) {
        UserInfoVo vo = new UserInfoVo();
        if (StringUtils.isBlank(userSn)) {
            log.warn("getUserInfoBySn fail, userSn is null or ''");
            return vo;
        }
        SystemUser user = entityManager.createNamedQuery("SystemUser.findByUserSn", SystemUser.class)
                .setParameter("userSn", userSn)
                .getSingleResult();
        if (user == null) {
            log.warn("getUserInfoBySn fail, user find null, userSn: {}", userSn);
            return vo;
        }
        userCopier.copy(user, vo, null);
        return vo;
    }
}
